package sys.Message_Sysytem.dao.impl;

import sys.Message_Sysytem.dao.MessageDao;
import sys.Message_Sysytem.utils.DBUtils;
import sys.Message_Sysytem.bean.Message;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MessageDaoImpl extends DBUtils implements MessageDao {

    @Override
    public List<Message> findAll() {
        ArrayList<Message> message = new ArrayList<Message>();
        try {
            String sql = "select * from message";
            resultSet = query(sql,null);
            while (resultSet.next()){
                Message message1 = new Message();
                message1.setId(resultSet.getString("id"));
                message1.setMessage(resultSet.getString("message"));
                message1.setTime(resultSet.getString("time"));
                message.add(message1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return message;
    }

    @Override
    public int addMessage(Message message) {
        int count = 0;
        try {
            String sql = "insert into message values(null,?,?) ";
            ArrayList params = new ArrayList();
            params.add(message.getMessage());
            params.add(message.getTime());
            count = update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return count;
    }
}
